import { Toast } from 'antd-mobile'
import * as _ from 'lodash'
import * as Libs from '../../utils/Libs'
import * as profitService from '../../services/home/profitServie'
import * as assetLiabilityService from '../../services/home/assetLiabilityService'
import { createAction, NavigationActions } from '../../utils'

const initState = {
  profitSummary: {
    yysr: {onRise: 0, showMoney: 0},
    yycb: {onRise: 0, showMoney: 0},
    yylr: {onRise: 0, showMoney: 0},
    lrze: {onRise: 0, showMoney: 0},
    jlr: {onRise: 0, showMoney: 0},
    times: ""
  },
  yj: "",
  gy: "",
  profitDetail: [],
  lineXX: {
    xAxisDatas: [],
    lineDatas: [],
  },
  // lineYysr: { //营业收入
  //   xAxisDatas: [],
  //   lineDatas: [],
  // },
  // lineYycb: { //营业成本
  //   xAxisDatas: [],
  //   lineDatas: [],
  // },
  // lineYylr: { //营业利润
  //   xAxisDatas: [],
  //   lineDatas: [],
  // },
  // lineLrze: { // 利润总额
  //   xAxisDatas: [],
  //   lineDatas: [],
  // },
  // lineJlr: { // 净利润
  //   xAxisDatas: [],
  //   lineDatas: [],
  // }
}

export default {
  namespace: 'profit',
  state: initState,
  reducers: {
    initState(state, { payload}) {
      return { ...initState}
    },
    changeState(state, { payload }) {
      return { ...state, ...payload }
    }
  },
  effects: {
    *getProfitDetailMsg({payload}, { call, select, put}) {
      let params = {}
      params.startTime = (payload.startTime || "").replace('.','-')
      params.endTime = (payload.endTime || "").replace('.','-')
      const resp = yield call(profitService.getProfitDetailMsg, params)
      if (resp.resultCode !== 200) {
        Toast.info(resp.msg, 1)
        return false
      }
      const respD = resp.data || []
      const yysr = respD.yysr || {}
      const yysrTotal = yysr.total || {}
      const yysrXq = yysr.xq || []
      const yycb = respD.yycb || {}
      const yycbTotal = yycb.total || {}
      const yycbXq = yycb.xq || []
      const yysrRespD = profitService.buildYySr("营业收入", yysrTotal, yysrXq, yycbTotal, yycbXq)

      const yycbRespD = profitService.buildD("营业成本", yycbTotal, yycbXq)

      const yylr = respD.yylr || {}
      const yylrTotal = yylr.total || {}
      const yylrXq = yylr.xq || []
      const yylrRespD = profitService.buildD("营业利润", yylrTotal, yylrXq)

      const lrze = respD.lrze || {}
      const lrzeTotal = lrze.total || {}
      const lrzeXq = lrze.xq || []
      const lrzeRespD = profitService.buildD("利润总额", lrzeTotal, lrzeXq)

      const jlr = respD.jlr || {}
      const jlrTotal = jlr.total || {}
      const jlrXq = jlr.xq || []
      const jlrRespD = profitService.buildD("净利润", jlrTotal, jlrXq)

      const profitDetails = []
      profitDetails.push(yysrRespD)
      profitDetails.push(yylrRespD)
      profitDetails.push(lrzeRespD)
      profitDetails.push(jlrRespD)

      yield put(createAction("changeState")({
        profitDetail: profitDetails
      }))
    },
    *getProfitMainMsg({payload}, { call, select, put}) {
      let params = {}
      params.startTime = (payload.startTime || "").replace('.','-')
      params.endTime = (payload.endTime || "").replace('.','-')
      const resp = yield call(profitService.getMainMsg, params)
      if (resp.resultCode !== 200) {
        Toast.info(resp.msg, 1)
        return false
      }
      const respData = resp.data || []
      const yysr = respData['yysr'] || {}
      const yysr_SHOWMONEY = Libs.numberUpperFormat(yysr.sumMoney, true)
      const yycb = respData['yycb'] || {}
      const yycb_SHOWMONEY = Libs.numberUpperFormat(yycb.sumMoney, true)
      const yylr = respData['yylr'] || {}
      const yylr_SHOWMONEY = Libs.numberUpperFormat(yylr.sumMoney, true)
      const lrze = respData['lrze'] || {}
      const lrze_SHOWMONEY = Libs.numberUpperFormat(lrze.sumMoney, true)
      const jlr = respData['jlr'] || {}
      const jlr_SHOWMONEY = Libs.numberUpperFormat(jlr.sumMoney, true)
      yield put(createAction("changeState")({
        profitSummary: {
          yysr: {onRise: yysr.onRise, showMoney: yysr_SHOWMONEY},
          yycb: {onRise: yycb.onRise, showMoney: yycb_SHOWMONEY},
          yylr: {onRise: yylr.onRise, showMoney: yylr_SHOWMONEY},
          lrze: {onRise: lrze.onRise, showMoney: lrze_SHOWMONEY},
          jlr: {onRise: jlr.onRise, showMoney: jlr_SHOWMONEY},
          times: respData.times
        }
      }))
    },
    *getContentMsg({payload}, {call, select, put}) {
      let params = {}
      params.startTime = (payload.startTime || "").replace('.','-')
      params.endTime = (payload.endTime || "").replace('.','-')
      params.type = 2
      const resp = yield call(assetLiabilityService.getShouyeContentMsg, params)
      if (resp.resultCode !== 200) {
        Toast.info(resp.msg, 1)
        return false
      }
      const respData = resp.data || {}
      const yj = (respData.yj || [])[0]
      const gy = (respData.gy || [])[0]
      console.log('yj ', yj, ' gy ', gy)
      yield put(createAction("changeState")({
        yj: yj,
        gy: gy
      }))
    },
    *getProfitCompare({payload}, { call, select, put}) {
      let params = {}
      params.startTime = (payload.startTime || "").replace('.','-')
      params.endTime = (payload.endTime || "").replace('.','-')
      params.useArea = payload.useArea
      const resp = yield call(profitService.getProfitModelCompare, params)
      const bizResp = resp.data || {}

      const lineD = bizResp.line || []
      let xAxisDatas = []
      let lineDatas = []
      let lastLineDatas = []
      _.map(lineD, (v) => {
        xAxisDatas.push({
          time: v.yearMoth,
          tb: v.onRise,
          hb: v.linkRise
        })
        lineDatas.push(v.sumMoney)
        lastLineDatas.push(v.tbSumMoney)
      })

      const xxParams = {
        xAxisDatas: xAxisDatas,
        lineDatas: [lineDatas,lastLineDatas],
      }
      yield put(createAction("changeState")({
        lineXX: xxParams
      }))
    }
  }
}
